/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2011 All Rights Reserved.
 */
package com.liwei.nio.mina;

import java.nio.charset.Charset;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 *
 * @author lidian
 * @version $Id: MyProtocalEncoder.java, v 0.1 2011-1-27 上午10:11:28 lidian Exp $
 */
public class MyProtocalEncoder extends ProtocolEncoderAdapter {
    private final Charset charset;

    public MyProtocalEncoder(Charset charset) {
        this.charset = charset;
    }

    //在此处实现对MyProtocalPack包的编码工作，并把它写入输出流中  
    public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
                                                                                    throws Exception {
        MyProtocalPack value = (MyProtocalPack) message;
        IoBuffer buf = IoBuffer.allocate(value.getLength());
        buf.setAutoExpand(true);
        buf.putInt(value.getLength());
        buf.put(value.getFlag());
        if (value.getContent() != null)
            buf.put(value.getContent().getBytes());
        buf.flip();
        out.write(buf);
    }

    public void dispose() throws Exception {
    }
}
